import tkinter as tk
import higher_superior
import system_administrator
import Monitoring_personnel
import Maintenance_personnel
import pymssql
import json

window = tk.Tk()
window.title("植物管理系统")
window.geometry("500x300")


def open_higher_superior(entry1_value):
    higher_superior.open(entry1_value)


def open_system_administrator(entry1_value):
    system_administrator.open(entry1_value)


def open_Monitoring_personnel(entry1_value):
    Monitoring_personnel.open(entry1_value)


def open_Maintenance_personnel(entry1_value):
    Maintenance_personnel.open(entry1_value)


def check_credentials(entry1_value):
    userid = entry2.get()
    password1 = entry3.get()
    name1 = entry1.get()
    conn = pymssql.connect(host='localhost', server='LAPTOP-QBHKFR9T', port='1433', user='Mysql',
                           password='15681743536',
                           database='植物管理系统', charset='utf8')

    if conn:
        print("连接成功!")
    cursor = conn.cursor()  # 获取游标
    sql = f"select 职位,姓名 from 角色 where 账号='{userid}' and 密码='{password1}' and 姓名='{name1}'"
    cursor.execute(sql)
    for row in cursor:  # 对每一行数据遍历输出

        print("%-4s,%-4s" % (row[0], row[1]))

    if row[0] == "上级主管部门":
        open_higher_superior(entry1.get())
    elif row[0] == "系统管理人员":
        open_system_administrator(entry1.get())
    elif row[0] == "监测人员":
        open_Monitoring_personnel(entry1.get())
    elif row[0] == "养护人员":
        open_Maintenance_personnel(entry1.get())
    else:
        print("用户名或密码错误，请重试！")


label1 = tk.Label(window, text="姓名")
entry1 = tk.Entry(window)

label2 = tk.Label(window, text="账号")
entry2 = tk.Entry(window)

label3 = tk.Label(window, text="密码")
entry3 = tk.Entry(window, show="*")

label4 = tk.Label(window, text="请选择角色！")

button1 = tk.Button(window, text="上级主管部门", command=lambda: check_credentials(entry1.get()), width=20, height=1)
button2 = tk.Button(window, text="系统管理员", command=lambda: check_credentials(entry1.get()), width=20, height=1)
button3 = tk.Button(window, text="养护人员", command=lambda: check_credentials(entry1.get()), width=20, height=1)
button4 = tk.Button(window, text="监测人员", command=lambda: check_credentials(entry1.get()), width=20, height=1)

label1.grid(row=0, column=1)
entry1.grid(row=0, column=2)
label2.grid(row=1, column=1)
entry2.grid(row=1, column=2)
label3.grid(row=2, column=1)
entry3.grid(row=2, column=2)
label4.grid(row=3, column=0)
button1.grid(row=4, column=0, sticky="nsew")
button2.grid(row=4, column=1, sticky="nsew")
button3.grid(row=4, column=2, sticky="nsew")
button4.grid(row=4, column=3, sticky="nsew")

window.columnconfigure(0, weight=1)
window.columnconfigure(1, weight=1)
window.columnconfigure(2, weight=1)

window.rowconfigure(0, weight=1)
window.rowconfigure(1, weight=1)
window.rowconfigure(2, weight=1)
window.rowconfigure(3, weight=1)
window.rowconfigure(4, weight=1)
window.rowconfigure(5, weight=1)

window.mainloop()
